package com.len.service;

import com.github.pagehelper.Page;
import com.len.entity.PayEntity;
import com.len.entity.UserEntity;
import com.len.util.DateUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class PayService {

    @Resource
    private MongoTemplate mongoTemplate;

    @Resource
    private PageService pageService;

    public Page<PayEntity> getList(PayEntity payEntity, Page<PayEntity> page) {
        Query query = new Query();
        query.skip((long) (page.getPageNum() - 1) *page.getPageSize());
        query.limit(page.getPageSize());

        Criteria criteria = new Criteria();
        criteria.and("status").is(1);
        if (StringUtils.isNotBlank(payEntity.getOrderId())){
            criteria.and("id").is(payEntity.getOrderId());
        }
        if (payEntity.getUserNO()!=null){
            UserEntity userEntity = mongoTemplate.findOne(Query.query(Criteria.where("userNO").is(payEntity.getUserNO())),UserEntity.class);
            if (userEntity!=null){
                criteria.and("userId").is(userEntity.getId());
            }
        }

        if (StringUtils.isNotBlank(payEntity.getBeginTime())&&StringUtils.isNotBlank(payEntity.getEndTime())){
            criteria.and("createTime").gte(Integer.valueOf(DateUtil.date2TimeStamp(payEntity.getBeginTime(),"yyyy-MM-dd"))).lt(Integer.parseInt(DateUtil.date2TimeStamp(payEntity.getEndTime(),"yyyy-MM-dd"))+24*3600);
        }else if (StringUtils.isNotBlank(payEntity.getBeginTime())){
            criteria.and("createTime").gte(Integer.valueOf(DateUtil.date2TimeStamp(payEntity.getBeginTime(),"yyyy-MM-dd")));
        }else if (StringUtils.isNotBlank(payEntity.getEndTime())){
            criteria.and("createTime").lt(Integer.parseInt(DateUtil.date2TimeStamp(payEntity.getEndTime(),"yyyy-MM-dd"))+24*3600);
        }

        Aggregation aggregation = Aggregation.newAggregation(
                //主表条件
                Aggregation.match(criteria),
                Aggregation.sort(Sort.by(Sort.Order.desc("createTime"),Sort.Order.desc("status"))),
                Aggregation.skip(query.getSkip()),
                Aggregation.limit(query.getLimit()),
                //联表
                Aggregation.lookup("user","userId","id","link"),
                //次表条件
                Aggregation.unwind("link"),
                Aggregation.project(PayEntity.class).and("link.userNO").as("userNO")
        );
        List<PayEntity> list = mongoTemplate.aggregate(aggregation,PayEntity.class, PayEntity.class).getMappedResults();
        query.addCriteria(criteria);

        long total = pageService.count(query, PayEntity.class);

        page.addAll(list);
        page.setTotal(total);
        return page;
    }


}
